var path = require('path')
var os = require('os')
var webpack = require('webpack')
var HtmlWebpackPlugin = require('html-webpack-plugin');
//var ExtractTextPlugin = require('extract-text-webpack-plugin');//css打包成独立文件
var WebpackMd5Hash = require('webpack-md5-hash');
//删除构建的dist目录
console.log("正在删除dist目录...");
require('shelljs/global')
rm('-rf', path.join(__dirname,'./dist'));

module.exports = {
  entry: {
    bundle: path.resolve(__dirname, './src/main.js'),
    vendor: ['vue','vuex','vue-router']//打包为 vendor.js的模块
  },//编译入口文件
  output: {
    path: path.resolve(__dirname, './dist/assets'), //打包目录
    publicPath: '/assets/', //静态资源url配置
    filename: 'js/build.[chunkhash].js',//整个应用的自定义组件、模块、业务代码
    chunkFilename: 'js/chunk/[name].[chunkhash].js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.js*$/,
        loader: 'babel-loader',
        exclude: function(path) {
            // 路径中含有 node_modules 的就不去解析。
            var isNpmModule = !!path.match(/node_modules/);
            // src/libs/request.js这个文件要特殊处理
            /*var isRequestLib = !!path.match(/src(\/|\\)libs(\/|\\)request\.js$/);
            return isNpmModule || isRequestLib ;*/
            return isNpmModule;
        },
        query: {
          compact: false,
          cacheDirectory: './webpack_cache',
        }
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: [
          'file-loader?limit=1000&name=img/[name]-[hash].[ext]',
          {
            loader: 'image-webpack-loader',
            query: {
              mozjpeg: {
                progressive: true,
              },
              gifsicle: {
                interlaced: true,
              },
              optipng: {
                optimizationLevel: 7,
              },
              pngquant: {
                quality: '65-90',
                speed: 4
              }
            }/*,//webpack2 升级这里的配置发生变化
            query: {
              progressive: true,
              optimizationLevel: 7,
              interlaced: false,
              pngquant: {
                quality: '65-90',
                speed: 4
              }
            } */
          }
        ]
      }

    ]
  },
  resolve: {
    extensions: ['.js','.vue','.jsx'], //自动不全后缀
    alias: {
      'vue$': 'vue/dist/vue.min.js',
      'vuex$': 'vuex/dist/vuex.min.js',
      'vue-router$': 'vue-router/dist/vue-router.min.js'
    },

  },
  performance: {
    hints: false
  },
  devtool : '#source-map',//生成sourcemap,便于开发调试
  plugins :[
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    // new webpack.DllReferencePlugin({
    //     context: __dirname,
    //     manifest: require('./js/manifest.json'),
    // }),
    new webpack.optimize.UglifyJsPlugin({ //压缩代码
      workers: os.cpus().length,
      sourceMap: true, //开启调试模式
      compress: {
        warnings: false, //删除警告
        drop_debugger: true,//去除debugger
        drop_console: true//去除console
      },
      // mangle: false  //所有的函数名进行混淆
    }),
    new webpack.LoaderOptionsPlugin({
      // 使用用户自定义插件
      minimize: true
    }),
    // new ExtractTextPlugin("styles.css"),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      filename: 'libs/vendor.[hash].js',//之前没有加hash，导致上线会白屏一段时间，因手机而异
      minChunks: Infinity
    }),
    new HtmlWebpackPlugin({ //根据模板插入css/js等生成最终HTML
        filename: path.resolve(__dirname,"./dist/index.html"), //生成的html存放路径，相对于 path
        template: path.resolve(__dirname,"./src/index.html"), //html模板路径
        //favicon:'./src/assets/img/favicon.ico', //根图标文件一定要加载，不然会跳转404
        inject:'body',
        minify:{                //压缩HTML文件
          removeComments:true,        //移除HTML中的注释
          collapseWhitespace:true     //删除空白符与换行符
        }
    }),
    new WebpackMd5Hash(),
    new webpack.NoErrorsPlugin()
  ]
}
